/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.config.Config;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.dao.RoleDao;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 用户Service
 * 
 * @author waiel23
 * @version 2019-04-11
 */
@Service
@Transactional(readOnly = true)
public class UserService extends CrudService<UserDao, User> {
	@Autowired
	private RoleDao roleDao;
	
	/**
	 * 根据ID获取用户
	 * 
	 * @param id
	 * @return 取不到返回null
	 */
	@Override
	public User get(String id) {
		User user = (User) CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_ID_ + id);
		if (user == null) {
			user = dao.get(id);
			if (user == null) {
				return null;
			}
			user.setRoleList(roleDao.findList(new Role(user)));
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_ID_ + user.getId(), user);
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
		}
		return user;
	}
	/**
	 * 获取用户
	 * @param id
	 * @return
	 */
	public User getUser(String id) {
		return this.get(id);
	}

	/**
	 * 根据登录名获取用户
	 * @param loginName
	 * @return
	 */
	public User getUserByLoginName(String loginName) {
		return this.getByLoginName(loginName);
	}
	

	/**
	 * 通过部门ID获取用户列表，仅返回用户id和name（树查询用户时用）
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<User> findUserByOfficeId(String officeId) {
		List<User> list = (List<User>)CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + officeId);
		if (list == null){
			User user = new User();
			user.setOffice(new Office(officeId));
			list = dao.findUserByOfficeId(user);
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + officeId, list);
		}
		return list;
	}
	
	/**
	 * 根据登录名获取用户
	 * 
	 * @param loginName
	 * @return 取不到返回null
	 */
	public User getByLoginName(String loginName) {
		User user = (User) CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_PHONE_ID + loginName);
		if (user == null) {
			user = dao.getByLoginName(new User(null, loginName));
			if (user == null) {
				return null;
			}
			user.setRoleList(roleDao.findList(new Role(user)));
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_PHONE_ID + user.getId(), user);
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
		}
		return user;
	}

	/**
	 * 根据电话号码获取用户
	 * @param phoneNumber
	 * @return
	 */
	public User getByPhoneNumber(String phoneNumber) {
		Object obj = CacheUtils.get(UserUtils.USER_CACHE, UserUtils.USER_CACHE_PHONE_ID + phoneNumber);
		User user = null;
		if (!Config.isDebugMode() && obj != null && obj instanceof User) {
			user = (User) obj;
		}
		if (user == null) {
			user = dao.getByPhoneNumber(phoneNumber);
			if (user == null) {
				return null;
			}
			user.setRoleList(roleDao.findList(new Role(user)));
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_PHONE_ID + phoneNumber, user);
			CacheUtils.put(UserUtils.USER_CACHE, UserUtils.USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
		}
		return user;
	}
	
	
	/**
	 * 根据userIds查询userList
	 * @param userIds
	 * @return
	 */
	public List<User> getListByUserIds(String userIds) {
		if(StringUtils.isBlank(userIds)){
			return null;
		}
		userIds = userIds.replace(",", "','");
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userIds", userIds);
		List<User> userList = dao.findListByUserIds(paramMap);
		return userList;
	}

}
